package cn.calm.repaymentNotice.service;

import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import cn.calm.repaymentNotice.R;
import cn.calm.repaymentNotice.client.MainActivity;
import cn.calm.repaymentNotice.client.RepaymentNoticeException;
import cn.calm.repaymentNotice.dao.DBService;
import cn.calm.repaymentNotice.entity.RepaymentNoticeInfo;
import cn.calm.repaymentNotice.util.DateUtil;
import cn.calm.repaymentNotice.util.NoticeInfoUtil;

public class RepaymentNoticeService extends Service {
	private Timer timer = new Timer();
	private TimerTask task;
	private static int i = 0;

	public RepaymentNoticeService() {
		task = new TimerTask() {
			@Override
			public void run() {
				
		        DBService service=DBService.getInstance(RepaymentNoticeService.this);
		        try {
					List<RepaymentNoticeInfo> listAll = service.listAll();
					Calendar today=Calendar.getInstance();
					today.set(Calendar.HOUR_OF_DAY, 0);
					today.set(Calendar.MINUTE, 0);
					today.set(Calendar.MILLISECOND, 0);
					today.set(Calendar.SECOND, 0);
					for(RepaymentNoticeInfo info :listAll){
						long days=DateUtil.subDays(info.predictRepaymentDate);
						if (days <= info.preNoticeDays) {
							NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);  
					        // 创建一个Notification  
					        Notification notification = new Notification(); 
					        // 设置显示在手机最上边的状态栏的图标  
					        notification.icon = R.drawable.icon;  
					        String infoStr= NoticeInfoUtil.info(info.predictRepaymentDate);
					        notification.tickerText= infoStr;
					       
					        // 当当前的notification被放到状态栏上的时候，提示内容  
					        notification.tickerText = String.valueOf(i);  
					          
					        /*** 
					         * notification.contentIntent:一个PendingIntent对象，当用户点击了状态栏上的图标时，该Intent会被触发 
					         * notification.contentView:我们可以不在状态栏放图标而是放一个view 
					         * notification.deleteIntent 当当前notification被移除时执行的intent 
					         * notification.vibrate 当手机震动时，震动周期设置 
					         */  
					        // 添加声音提示  
//					        notification.defaults=Notification.DEFAULT_SOUND;  
					        // audioStreamType的值必须AudioManager中的值，代表着响铃的模式  
//					        notification.audioStreamType= android.media.AudioManager.ADJUST_LOWER;  
					          
					        //下边的两个方式可以添加音乐  
					        //notification.sound = Uri.parse("file:///sdcard/notification/ringer.mp3");   
					        //notification.sound = Uri.withAppendedPath(Audio.Media.INTERNAL_CONTENT_URI, "6");   
					        Intent intent = new Intent(RepaymentNoticeService.this, MainActivity.class);  
					        PendingIntent pendingIntent = PendingIntent.getActivity(RepaymentNoticeService.this, 0, intent, PendingIntent.FLAG_ONE_SHOT);  
					        // 点击状态栏的图标出现的提示信息设置  
					        notification.setLatestEventInfo(RepaymentNoticeService.this, "还款提醒",infoStr, pendingIntent);  
					        manager.notify(1, notification);  
					        return;
						}
					}
				} catch (RepaymentNoticeException e) {
					e.printStackTrace();
				}
		       
			}
		};
	}

	@Override
	public IBinder onBind(Intent intent) {
		return null;
	}

	@Override
	public void onCreate() {
		super.onCreate();
		Date date = new Date();
		timer.schedule(task, date, 86400000);
	}
}
